import moment from "moment";
import type { RangePickerProps } from "antd/es/date-picker/generatePicker";

type RangePickerValue = RangePickerProps<moment.Moment>["value"];

export function fixedZero(val: number) {
  return val * 1 < 10 ? `0${val}` : val;
}

export function getUrlParam(name: string) {
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  let r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}

export function getFirstDayAndLastDay(grantCycle: string) {
  var date = new Date(grantCycle),
    y = date.getFullYear(),
    m = date.getMonth();
  var firstDay = moment(new Date(y, m, 1)).format("YYYY-MM-DD");
  var lastDay = moment(new Date(y, m + 1, 0)).format("YYYY-MM-DD");
  return {
    firstDay,
    lastDay,
  };
}

export function getTimeDistance(
  type: "today" | "week" | "month" | "year"
): RangePickerValue {
  const now = new Date();
  const oneDay = 1000 * 60 * 60 * 24;

  if (type === "today") {
    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);
    return [moment(now), moment(now.getTime() + (oneDay - 1000))];
  }

  if (type === "week") {
    let day = now.getDay();
    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);

    if (day === 0) {
      day = 6;
    } else {
      day -= 1;
    }

    const beginTime = now.getTime() - day * oneDay;

    return [moment(beginTime), moment(beginTime + (7 * oneDay - 1000))];
  }
  const year = now.getFullYear();

  if (type === "month") {
    const month = now.getMonth();
    const nextDate = moment(now).add(1, "months");
    const nextYear = nextDate.year();
    const nextMonth = nextDate.month();

    return [
      moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`),
      moment(
        moment(
          `${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`
        ).valueOf() - 1000
      ),
    ];
  }

  return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
}

/**
 * 根据给定的索引生成对应的字母
 * @param index - 索引（从0开始）
 * @returns 对应的字母
 */
export function indexToLetter(index: number): string {
  // 确保输入是非负整数
  if (index < 0) {
    throw new Error('Index must be a non-negative integer');
  }
  // 计算字母
  return String.fromCharCode(index + 65);
}

/**
 * Shortens a long string by keeping the first and last few characters, 
 * and replacing the middle portion with "...".
 * 
 * @param {string} input - The input string to be shortened.
 * @param {number} [lengthStart=3] - The number of characters to keep the start of the string. Defaults to 3.
 * @param {number} [lengthEnd=4] - The number of characters to keep the end of the string. Defaults to 4.
 * @returns {string} - The shortened string with the middle replaced by "***", or the original string if it's short enough.
 */
export function shortenString(input: string, lengthStart: number = 3, lengthEnd: number = 4): string {
  if (input.length <= (lengthStart +lengthEnd)) {
    return input; // 如果字符串长度小于等于前后保留字符总和，直接返回原字符串
  }
  
  const start = input.slice(0, lengthStart);
  const end = input.slice(-lengthEnd);
  return `${start}***${end}`;
}